#include "data_acquisition.h"
#include "time.h"
SAMPLE_TIME adc_sample_time;
uint8_t data_stored_flag;  // 数据存储标志位
//led状态存储数组
uint8_t ucled[6] = {1,0,1,0,1,0};
/**
 * @brief led任务函数
 * @param none
 * @return none
 */
uint32_t blink_time;
void led_task(void)
{
		if(adc_run_state == 1)//采样开启
		{
			//判断是否到达闪烁时间
			if(uwTick - blink_time >= 500)
			{
				//翻转状态
				ucled[0] ^= 1;
				//更新时间戳
				blink_time = uwTick;
			}	
		}
		else//采样关闭
		{
			ucled[0] = 0;
			blink_time = 0;
		}
    led_disp(ucled);
}
/**
 * @brief 按键任务函数
 * @param none
 * @return none
 */
//按键读取变量
uint8_t key_val,key_old,key_down,key_up;
void key_task(void)
{
    key_val = key_read();
    key_down = key_val & (key_old ^ key_val);
    key_up = ~key_val & (key_old ^ key_val);
    key_old = key_val;

    switch(key_down)
    {
			case 1://按键启停
				adc_run_state^=1;
				if(adc_run_state == 0)//停止状态
				{
					user_printf(&huart3,"Periodic Sampling STOP\r\n");
					storage_system_close();
					log_operation("sample stop (key press)", " ");
				}
				else if(adc_run_state == 1)//启动状态
				{
					user_printf(&huart3,"Periodic Sampling\r\n");
					user_printf(&huart3,"sample cycle:%ds\r\n",adc_sample_time);
				}
			break;
				
			case 2:
				adc_sample_time = SAMPLE_TIME_5s;
				char sample_time_2[256];
				sprintf(sample_time_2, "sample:%d",(int)adc_sample_time);
				flash_write(CONFIG_ADDR_2,sample_time_2);	
				user_printf(&huart3,"sample cycle adjust: %ds\r\n",adc_sample_time);
				//日志
				char log_msg_key_2[128];
				sprintf(log_msg_key_2, "%ds (keypress)", adc_sample_time);
				log_operation("cycle switch to ", log_msg_key_2);
			break;
			
			case 3:
				adc_sample_time = SAMPLE_TIME_10s;
				char sample_time_3[256];
				sprintf(sample_time_3, "sample:%d",(int)adc_sample_time);
				flash_write(CONFIG_ADDR_2,sample_time_3);	
				user_printf(&huart3,"sample cycle adjust: %ds\r\n",adc_sample_time);
			
				//日志
				char log_msg_key_3[128];
				sprintf(log_msg_key_3, "%ds (keypress)", adc_sample_time);
				log_operation("cycle switch to ", log_msg_key_3);
			break;
			
			case 4:
				adc_sample_time = SAMPLE_TIME_15s;
				char sample_time_4[256];
				sprintf(sample_time_4, "sample:%d",(int)adc_sample_time);
				flash_write(CONFIG_ADDR_2,sample_time_4);	
				user_printf(&huart3,"sample cycle adjust: %ds\r\n",adc_sample_time);
			
				//日志
				char log_msg_key_4[128];
				sprintf(log_msg_key_4, "%ds (keypress)", adc_sample_time);
				log_operation("cycle switch to ", log_msg_key_4);
			break;
    }
}
///@brief 采样运行函数
uint8_t adc_run_state;
uint8_t output_time_type;//输出格式标志位
uint32_t adc_out_time = 0;
void adc_run_func(uint8_t state)
{
	if(state == 0)//停止周期采样
	{
		adc_out_time = 0;
		oled_printf(0,0,"  system idle                    ");
		ucled[1] = 0;
	}
	else if(state == 1)//开启周期采样
	{
		RTC_TimeTypeDef oled_time;
		RTC_DateTypeDef oled_date;
		rtc_get_time_info(&oled_time,&oled_date);
		
		uint32_t c_time = oled_time.Hours * 3600 + oled_time.Minutes * 60 + oled_time.Seconds;
		//user_printf(&huart3,"%ld\r\n",c_time);
		//到达采样时间
		if(c_time - adc_out_time >= adc_sample_time)
		{
			data_stored_flag = 1;
			volatage = v_get * user_ctr.ratio;
			//超限判断
			uint8_t over_flag = 0;//超限标志位
			if(volatage > user_ctr.limit)
			{
				ucled[1] = 1;
				over_flag = 1;//拉高超限标志位
			}
			else
			{
				ucled[1] = 0;
				over_flag = 0;//拉低超限标志位
			}
			//打印当前时间
			if(output_time_type == 0)//正常格式
			{
				if(over_flag == 1)
				{
					rtc_print_current_time();
					user_printf(&huart3," ch0=%.2fV OverLimit(%.2f) !\r\n",volatage,user_ctr.limit);
				}
				else//未超限
				{
					rtc_print_current_time();
					user_printf(&huart3," ch0=%.2fV\r\n",volatage);
				}
			}
			else if(output_time_type == 1)//加密格式
			{
				struct tm time_info;
				time_info.tm_year = oled_date.Year + 2000 - 1900;
				time_info.tm_mon = oled_date.Month - 1;
				time_info.tm_mday = oled_date.Date;
				time_info.tm_hour = oled_time.Hours;
				time_info.tm_min = oled_time.Minutes;
				time_info.tm_sec = oled_time.Seconds;
				//转换Unix时间戳
				time_t timestamp = mktime(&time_info);
				timestamp -= 8 * 3600;
				uint16_t integer_part = (uint16_t)volatage; 
				// 获取小数部分
				float fractional_part = (volatage - (float)integer_part);
				// 将小数部分乘以65536
				uint16_t fractional_value = (uint16_t)(fractional_part * 65536.f);
				if(over_flag == 0)//未超限
				{
					user_printf(&huart3,"%08X%04X%04X\r\n",timestamp,integer_part,fractional_value);
				}
				else//超限
				{
					user_printf(&huart3,"%08X%04X%04X*\r\n",timestamp,integer_part,fractional_value);
				}
			}
			
			//oled输出
			oled_printf(0,0,"%02d:%02d:%02d        %.2fV",
              oled_time.Hours,
              oled_time.Minutes,
              oled_time.Seconds,
							volatage);
			
			//user_printf(&huart3,"进入采样\r\n");
			//uint32_t timestamp = get_current_timestamp();
      //store_sampling_data(volatage, user_ctr.limit, timestamp);
			//更新时间戳
			adc_out_time = c_time;
		}
	}
}
///@brief 数据管理任务
void data_config(void)
{
	//adc采样系统
	adc_run_func(adc_run_state);
}